<?php
class ControllerAccountRechargeablecard extends AccountController {
	     
  	public function index() {//recharge()
      M('account/rechargeablecard'); 

    	$this->document->setTitle($this->_['heading_title']);
        $this->document->addScript('static/javascript/jquery/jquery.validate.js');
		
    	if ($this->request->isPost() && $this->validate()) {

		  M('account/customer');
          M('account/rechargeablecard'); 
          M('total/credit');

		  $customer_info=$this->model_account_customer->getCustomer($this->customer->getId());
          $card_info=$this->model_account_rechargeablecard->getRechargeablecard($this->request->post['cardNumber']);

          $order_info['customer_id']=$this->customer->getId();
          $order_info['order_id']=0;
          $order_info['description']=$this->_['rechargeablecard'].$card_info['cardNumber'].$this->_['description'];
          $order_total['value']=$card_info['cardAmount']+$this->getCoupon($card_info['cardAmount']);
          $this->model_total_credit->confirmCard($order_info, $order_total);

   
          $card_info['cardCustomer']=$customer_info['email'];
          $card_info['cardStatus']=1;
          $this->model_account_rechargeablecard->editRechargeablecard($card_info['cardNumber'], $card_info);
    
    
          $this->session->set_flashdata('success', $this->_['text_success']);	  
	  		  $this->redirect(US('account/account'));

    	}
		

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->_['heading_title'],
			    'href'      => US('account/rechargeablecard'),
        	'separator' => $this->_['text_separator']
      	);		


      	   M('account/rechargeablecard');
          $results=$this->model_account_rechargeablecard->getCouponcards();
           foreach($results as $result){
             $this->data['couponcards'][]=array(
             	'lowAmount'=>$result['lowAmount'],
             	'highAmount'=>$result['highAmount'],
             	'reward'=>$result['reward']
             	);
           }
	  

	   	  $this->data['cardNumber'] = P('cardNumber');
		  $this->data['cardCode'] = P('cardCode');
		  $this->display('account/rechargeablecard.tpl');
  	}
  


  	protected function validate() {

      M('account/rechargeablecard');

        M('account/rechargeablecard');
       $card_info = $this->model_account_rechargeablecard->getRechargeablecard($this->request->post['cardNumber']);
 
       if(strlen($this->request->post['cardNumber'])!=12||(!is_numeric($this->request->post['cardNumber']))){
          $this->setMessage('error_cardNumber', $this->_['error_cardNumber']);
          return false;
       }

       if($card_info['cardStatus']==1){
          $this->setMessage('error_cardNumber', $this->_['error_cardNumber1']);
           return false;
       }

     
       if (!empty($card_info)&&($card_info['cardCode']==$this->request->post['cardCode'])){
        return true;
       }

       $this->setMessage('error_cardCode', $this->_['error_cardCode']);
       return false;
  	}


    protected function getCoupon($cardAmount){
        M('account/rechargeablecard');

        $results=$this->model_account_rechargeablecard->getCouponcards();
        foreach($results as $result){
            if($result['lowAmount']<=$cardAmount&&$cardAmount<=$result['highAmount']){
                return $result['reward'];
            }
        }
       return 0;
    }
}
?>
